<script>
import './styles/index.scss' // 公共的样式
import { mapActions, mapGetters } from 'vuex'
// #ifdef H5
import Bridge from '@/plugins/JSBridge'
import { requestOpenId } from '@/utils/tools.js'
// #endif
import { getMiniOpenId } from '@/utils/tools.js'

export default {
  onLaunch: function(options) {
    console.log('share_code:',uni.getStorageSync('share_code'));
    console.log('onLaunch',options);
    // if(options.query.share_code){
    //   uni.setStorageSync('share_code', options.query.share_code)
    // }else{
    //   uni.removeStorageSync('share_code');
    // }
    uni.getSystemInfo({
      success: res => {
        this.$store.dispatch('SET_SYSTEM_INFO', {
          statusBarHeight: res.statusBarHeight,
          screenWidth: res.screenWidth,
          screenHeight: res.screenHeight,
          SDKVersion: res.SDKVersion
        })
      }
    })

    // 清除一些临时缓存
    uni.removeStorageSync('pageAfterLogin')

    if (options.path && !options.path.match('pages/share/index') && options.scene !== 1037 && options.scene !== 1038) {
      const token = uni.getStorageSync('biXuanHello')
      if (token) {
        // #ifdef H5
        if (Bridge.isInApp) {
          return
        }
        if (Bridge.isInWeChat) {
          console.log("isInWeChat");
          requestOpenId()
        }
        // #endif
        getMiniOpenId();//获取小程序openId
        this.$api.commonApi.refreshToken().then(res => {
          if (res.success && res.data && res.data.token) {
            this.SET_TOKEN(res.data.token)
          }
        })
      }
    }
    uni.setStorage({
      key: 'isOnLaunch',
      data: true
    })
    console.log('App Launch')
  },
  onShow: function(options) {
    console.log('App onShow')
    this.getOtherMiniData(options, 1)
  },
  onHide: function() {
    console.log('App Hide')
  },
  onError: function(err) {
    // 这里只能捕获方法内的异常，不能捕获生命周期中的逻辑异常
    try {
      var info = JSON.stringify(err)
      var arr = info.split('\\n')
      this.$errorHandler(err, '', `微信触发的异常:${arr[1]} ${info}`)
    } catch (e) {
      console.log(e, '上报异常失败')
    }
  },
  computed: {
    ...mapGetters(['extraData', 'userInfo'])
  },
  methods: {
    ...mapActions(['setExtraData', 'SET_TOKEN']),
    toSensor: function(url) {
      let page = ''
      if (url && url.match('cart')) {
        page = 'send'
      } else if (url && url.match('mine')) {
        page = 'mine'
      }

      // 埋点
      if (page) {
        this.$sensorTrack('CLICKBUTTON', {
          page_name: 'gift_bag',
          button_name: page
        })
      }
    },
    getOtherMiniData(options, type) {
      // console.log(options)
      // #ifdef MP-WEIXIN
      if (options.scene === 1037 || options.scene === 1038) {
        if (options && options.referrerInfo && options.referrerInfo.extraData) {
          const extraData = options.referrerInfo.extraData
          if (type === 0) {
            if (extraData.token) {
              uni.setStorageSync('biXuanHello', extraData.token)
              return extraData.token
            }
          } else if (type === 1) {
            this.setExtraData(extraData)
            if (!extraData.hasBack && extraData.toPath && !extraData.toPath.match(options.path)) {
              uni.redirectTo({
                url: '/pagesOther/middle/index'
              })
            } else if (!extraData.hasBack && !extraData.toPath) {
              this.setExtraData(null)
            }
          }
        }
      } else {
        this.setExtraData(null)
      }
      return ''
      // #endif
    }
  }
}
</script>
<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "uview-ui/index.scss";
  // @import 'vant/lib/index.css';
</style>